package com.mydroidchat.data.net;

import com.mydroidchat.data.ChatMessage;
import com.mydroidchat.data.ChatUser;
import com.mydroidchat.data.Util;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageResponse extends ApiResponse {
    private static final Pattern messagesPattern = Pattern.compile("\\{\"user\":\"([^\"]+)\",\"time\":\"([^\"]+)\",\"text\":\"([^\"]+)\"}");
    private Set<ChatMessage> messages = new HashSet<ChatMessage>();
    
    public MessageResponse(String completePage) {
        super(completePage);

        Matcher allMatcher = messagesPattern.matcher(completePage);
        while (allMatcher.find()) {
            String user = Util.unescapeUnicode(allMatcher.group(1));
            String time = allMatcher.group(2);
            String message = Util.unescapeUnicode(allMatcher.group(3));

            messages.add(new ChatMessage(parseDate(time), message, ChatUser.getByName(user)));
        }
    }
    
    private Long parseDate(String date) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(date.substring(0, 2)));
        calendar.set(Calendar.MINUTE, Integer.parseInt(date.substring(3)));
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime().getTime();
    }

    public Set<ChatMessage> getMessages() {
        return messages;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String text = "[{\"user\":\"DeTray\",\"time\":\"16:07\",\"text\":\"\\u0444\\u0438\\u0433\\u0430\\u0441\\u0435.renovatio \\u0437\\u0430 5 \\u0434\\u043d\\u0435\\u0439 \\u0441\\u043a\\u0430\\u0447\\u0430\\u043b\\u0438 663 \\u0440\\u0430\\u0437\\u0430\"}{\"user\":\"DeTray\",\"time\":\"16:06\",\"text\":\"\\u0445\\u0435\\u0445.\\u043e\\u043d\\u043e \\u0435\\u0449\\u0451 \\u0431\\u0443\\u0434\\u0435\\u0442 \\u0434\\u043e\\u0440\\u0430\\u0431\\u043e\\u0442\\u0430\\u043d\\u043e. \\u043d\\u0430\\u0448 \\u0430\\u0434\\u043c\\u0438\\u043d \\u0437\\u0430\\u0431\\u044b\\u043b \\u043c\\u0435\\u043d\\u044f \\u0441\\u043f\\u0440\\u043e\\u0441\\u0438\\u0442\\u044c \\u0447\\u0451 \\u043a\\u0430\\u043a)))\"}{\"user\":\"monksis97\",\"time\":\"16:05\",\"text\":\"\\u043e\\u0431\\u043d\\u043e\\u0432\\u043b\\u0435\\u043d\\u043d\\u043e\\u0435 \\u043b\\u043e\\u0433\\u043e \\u0438 \\u0447\\u0430\\u0442 \\u043e\\u0442\\u043b\\u0438\\u0447\\u043d\\u043e \\u0441\\u043c\\u043e\\u0442\\u0440\\u044f\\u0442\\u0441\\u044f)\"}{\"user\":\"DeTray\",\"time\":\"16:02\",\"text\":\"\\u0447\\u0451 \\u043a\\u0430\\u043a?\"}{\"user\":\"kadavr\",\"time\":\"16:01\",\"text\":\"\\u0445\\u044b, \\u0437\\u0430\\u043b\\u0435\\u0437 \\u0447\\u0435\\u0440\\u0435\\u0437 \\u0434\\u0435\\u0441\\u043a\\u0442\\u043e\\u043f\\u043d\\u0443\\u044e \\u0432\\u0435\\u0440\\u0441\\u0438\\u044e)\"}{\"user\":\"dejavu\",\"time\":\"13:39\",\"text\":\"riartem, \\u043d\\u043e\\u0440\\u043c\"}{\"user\":\"sxs\",\"time\":\"13:14\",\"text\":\"\\u041e, \\u043d\\u043e\\u0432\\u043e\\u0435 \\u043b\\u043e\\u0433\\u043e, \\u043a\\u043b\\u0430\\u0441\\u0441)\"}{\"user\":\"riartem\",\"time\":\"11:23\",\"text\":\"dejavu, \\u043d\\u0443 \\u0447\\u0442\\u043e?\"}{\"user\":\"riartem\",\"time\":\"11:22\",\"text\":\"\\u041e\\u0431\\u043d\\u043e\\u0432\\u043b\\u0451\\u043d \\u0447\\u0430\\u0442 \\u043d\\u0430 \\u0441\\u0430\\u0439\\u0442\\u0435 \\u0434\\u043e \\u043f\\u043e\\u0441\\u043b\\u0435\\u0434\\u043d\\u0435\\u0439 \\u0432\\u0435\\u0440\\u0441\\u0438\\u0438\"}{\"user\":\"riartem\",\"time\":\"11:22\",\"text\":\"\\u0412\\u0441\\u0451 \\u0443\\u0436\\u0435 \\u043d\\u043e\\u0440\\u043c\\u0430\\u043b\\u044c\\u043d\\u043e\"}{\"user\":\"riartem\",\"time\":\"11:22\",\"text\":\"\\u041e\\u0431\\u043d\\u043e\\u0432\\u0438 \\u043a\\u044d\\u0448 \\u0431\\u0440\\u0430\\u0443\\u0437\\u0435\\u0440\\u0430\"}{\"user\":\"dejavu\",\"time\":\"11:18\",\"text\":\"http:\\/\\/s2.ipicture.ru\\/uploads\\/20120109\\/REtoe3xc.png \\u0441\\u043c\\u043e\\u0442\\u0440\\u0438\"}{\"user\":\"dejavu\",\"time\":\"11:17\",\"text\":\"\\u0441\\u0447\\u0430\\u0441 \\u043f\\u043e\\u043a\\u0430\\u0436\\u0443 \\u043a\\u0430\\u043a \\u0443 \\u043c\\u0435\\u043d\\u044f\"}{\"user\":\"riartem\",\"time\":\"11:17\",\"text\":\"\\u0449\\u0430\\u0441 \\u043f\\u0440\\u043e\\u0432\\u0435\\u0440\\u044e))\"}{\"user\":\"riartem\",\"time\":\"11:17\",\"text\":\"\\u043d\\u0435 \\u0437\\u043d\\u0430\\u044e\"}{\"user\":\"dejavu\",\"time\":\"11:16\",\"text\":\"\\u0434\\u0430\"}{\"user\":\"riartem\",\"time\":\"11:16\",\"text\":\"\\u041d\\u0430 \\u0433\\u043b\\u0430\\u0432\\u043d\\u043e\\u0439 \\u0441\\u0442\\u0440\\u0430\\u043d\\u0438\\u0446\\u0435?\"}{\"user\":\"riartem\",\"time\":\"11:16\",\"text\":\"\\u043a\\u0430\\u043a\\u043e\\u0439?\"}{\"user\":\"dejavu\",\"time\":\"11:16\",\"text\":\"\\u0430 \\u0433\\u0434\\u0435 \\u0447\\u0430\\u0442 \\u0434\\u0435\\u043b\\u0441\\u044f?\"}{\"user\":\"riartem\",\"time\":\"10:33\",\"text\":\"\\u0433\\u0434\\u0435 \\u0440\\u0435\\u043a\\u043b\\u0430\\u043c\\u0430?\"}{\"user\":\"Dron_Off\",\"time\":\"9:48\",\"text\":\"\\u041d\\u0430 \\u0432\\u0435\\u0441\\u044c \\u0447\\u0430\\u0442\"}{\"user\":\"Dron_Off\",\"time\":\"9:48\",\"text\":\"\\u0424\\u0438\\u0433\\u0430\\u0441\\u0441\\u0435 \\u0440\\u0435\\u043a\\u043b\\u0430\\u043c\\u0430 :)\"}{\"user\":\"riartem\",\"time\":\"9:38\",\"text\":\"\\u0412 \\u043e\\u0431\\u0449\\u0435\\u043c, \\u0437\\u0430\\u0432\\u0435\\u043b\\u0438 \\u043c\\u044b \\u0432 \\u0441\\u0435\\u0440\\u0432\\u0438\\u0441\\u0435 Google Moderator \\u0441\\u0432\\u043e\\u044e \\u0441\\u0442\\u0440\\u0430\\u043d\\u0438\\u0447\\u043a\\u0443. \\u0417\\u0430\\u0439\\u0442\\u0438 \\u0442\\u0443\\u0434\\u0430 \\u043c\\u043e\\u0436\\u043d\\u043e \\u0441 \\u0433\\u043b\\u0430\\u0432\\u043d\\u043e\\u0433\\u043e \\u043c\\u0435\\u043d\\u044e \\u0444\\u043e\\u0440\\u0443\\u043c\\u0430. http:\\/\\/mydroid.by\\/go\\/5q\"}{\"user\":\"Dron_Off\",\"time\":\"8:19\",\"text\":\":) \\u0423\\u0436\\u0435 \\u043d\\u0430 \\u0440\\u0430\\u0431\\u043e\\u0442\\u0435\"}{\"user\":\"riartem\",\"time\":\"8:16\",\"text\":\"\\u041c\\u0430\\u043d\\u044c\\u044f\\u043a\\u0438, \\u0432\\u0430\\u043c \\u043d\\u0430 \\u0440\\u0430\\u0431\\u043e\\u0442\\u0443 \\u043d\\u0435 \\u043d\\u0430\\u0434\\u043e \\u0447\\u0442\\u043e\\u043b\\u0435?)))\"}{\"user\":\"Dron_Off\",\"time\":\"3:32\",\"text\":\"\\u041f\\u0440\\u0438\\u0432\\u0435\\u0442\"}{\"user\":\"DeTray\",\"time\":\"3:02\",\"text\":\"Hi\"}{\"user\":\"Veris\",\"time\":\"1:57\",\"text\":\"\\u0418\\u0445\\u0430\\u0430\\u0430\\u0430! \\u0412\\u0441\\u0435\\u043c \\u043f\\u0440\\u0438\\u0432\\u0435\\u0442 \\u043f\\u043e\\u0441\\u0430\\u043d\\u044b!\"}{\"user\":\"DeTray\",\"time\":\"1:34\",\"text\":\"\\u0433\\u043b\\u0430\\u0432\\u043d\\u043e\\u0435 \\u0447\\u0442\\u043e \\u043c\\u044b \\u0435\\u0441\\u0442\\u044c!\"}{\"user\":\"DeTray\",\"time\":\"1:34\",\"text\":\"\\u0434\\u0430 \\u0438 \\u043f\\u043e\\u0445)))\"}{\"user\":\"footbaler\",\"time\":\"1:13\",\"text\":\"\\u0437\\u0430\\u0432\\u0438\\u0441\\u0442\\u044c - \\u043f\\u043b\\u043e\\u0445\\u043e\\u0435\\u0447\\u0443\\u0432\\u0441\\u0442\\u0432\\u043e, \\u0436\\u0430\\u043b\\u043a\\u043e \\u0442\\u0430\\u043a\\u0438\\u0445 \\u043b\\u044e\\u0434\\u0435\\u0439)\"}{\"user\":\"riartem\",\"time\":\"1:12\",\"text\":\"\\u0445\\u043e\\u0440\\u043e\\u0448\\u0438\\u0435 \\u043d\\u043e\\u0432\\u043e\\u0441\\u0442\\u0438: \\u043d\\u0430\\u0448 \\u0447\\u0430\\u0442 \\u0441\\u043a\\u0430\\u0447\\u0430\\u043b\\u0438 \\u0441  \\u043c\\u0430\\u0440\\u043a\\u0435\\u0442\\u0430 \\u0431\\u043e\\u043b\\u0435\\u0435 50 \\u0440\\u0430\\u0437. \\u0438 \\u0432\\u043e\\u0442 \\u043a\\u0430\\u043a\\u043e\\u0439 \\u0442\\u043e \\u043a\\u0430\\u0441\\u0442\\u0440\\u0430\\u0442 \\u0441 \\u0445\\u0431\\u0444 \\u0432\\u043b\\u0435\\u043f\\u0438\\u043b \\u043d\\u0430\\u043c \\u0435\\u0434\\u0438\\u043d\\u0438\\u0446\\u0443...\"}{\"user\":\"DeTray\",\"time\":\"23:47\",\"text\":\"\\u043a\\u043e\\u043d\\u0435\\u0447\\u043d\\u043e.\\u0434\\u0430 \\u0438 \\u043b\\u0430\\u0433\\u043e\\u0432 \\u0443 \\u043d\\u0435\\u0435\"}{\"user\":\"Hitmanby\",\"time\":\"23:46\",\"text\":\"psp vita \\u0442\\u0430\\u043a\\u0438\\u043c\\u0438 \\u0442\\u0435\\u043c\\u043f\\u0430\\u043c\\u0438 \\u0440\\u0430\\u0437\\u0432\\u0438\\u0442\\u0438\\u044f \\u0441\\u043c\\u0430\\u0440\\u0442\\u0444\\u043e\\u043d\\u043e\\u0432 \\u0442\\u043e\\u0447\\u043d\\u043e \\u0432 \\u043f\\u0440\\u043e\\u043b\\u0451\\u0442\\u0435\"}{\"user\":\"Hitmanby\",\"time\":\"23:45\",\"text\":\"))))))))))))))))))))))\"}{\"user\":\"DeTray\",\"time\":\"23:45\",\"text\":\"\\u0433\\u0435\\u0440\\u043e\\u0439 \\u0432 \\u0441\\u043c\\u044b\\u0441\\u043b\\u0435\\u044d\"}{\"user\":\"DeTray\",\"time\":\"23:45\",\"text\":\"\\u0447\\u0443\\u0432\\u0430\\u043a \\u043f\\u0440\\u043e\\u0440\\u0438\\u0441\\u043e\\u0432\\u0430 \\u0438\\u0434\\u0435\\u0430\\u043b\\u044c\\u043d\\u043e\"}{\"user\":\"DeTray\",\"time\":\"23:45\",\"text\":\"\\u0430 \\u044f \\u0432 \\u043d\\u0435\\u0451 \\u0443\\u0436\\u0435 \\u0440\\u0443\\u0431\\u0430\\u044e\\u0441\\u044c)))\"}{\"user\":\"Hitmanby\",\"time\":\"23:45\",\"text\":\"\\u044f \\u0432 \\u0448\\u043e\\u043a\\u0435 \\u043e\\u0442 \\u0433\\u0440\\u0430\\u0444\\u0438\\u043a\\u0438\"}{\"user\":\"Hitmanby\",\"time\":\"23:44\",\"text\":\"http:\\/\\/www.youtube.com\\/watch?feature=player_embedded&amp;v=16cToLf5YoY\"}{\"user\":\"monksis97\",\"time\":\"23:12\",\"text\":\"Hitmanby, \\u0443\\u0434\\u0430\\u043b\\u0438 \\u043d\\u0430\\u0444\\u0438\\u0433 \\u043c\\u0430\\u0440\\u043a\\u0435\\u0442 \\u0438 \\u043f\\u043e\\u0441\\u0442\\u0430\\u0432\\u044c \\u0437\\u0430\\u043d\\u043e\\u0432\\u043e...\\u043c\\u043e\\u0436\\u043d\\u043e \\u0438 \\u043d\\u0435 \\u0432 \\u0441\\u0438\\u0441\\u0442\\u0435\\u043c\\u043d\\u044b\\u0435)\"}{\"user\":\"Hitmanby\",\"time\":\"23:11\",\"text\":\"\\u043e\\u043d \\u0441\\u0442\\u0430\\u043b \\u043b\\u0430\\u0433\\u0430\\u0442\\u044c \\u043f\\u043e\\u0441\\u043b\\u0435 \\u0442\\u043e\\u0433\\u043e \\u043a\\u0430\\u043a \\u0441\\u0430\\u043c \\u043e\\u0431\\u043d\\u043e\\u0432\\u0438\\u043b\\u0441\\u044f\"}{\"user\":\"Hitmanby\",\"time\":\"23:11\",\"text\":\"\\u0430 \\u0435\\u0433\\u043e \\u043c\\u043e\\u0436\\u043d\\u043e \\u043f\\u0435\\u0440\\u0435\\u0443\\u0441\\u0442\\u0430\\u043d\\u043e\\u0432\\u0438\\u0442\\u044c?\"}{\"user\":\"Hitmanby\",\"time\":\"23:10\",\"text\":\"\\u0441\\u0442\\u043e\\u0438\\u0442 3.4.4\"}{\"user\":\"DeTray\",\"time\":\"23:08\",\"text\":\"\\u0438 \\u043d\\u0438\\u0447\\u0435\\u0433\\u043e((((\\u0435\\u0433\\u043e \\u0432\\u043e\\u043e\\u0431\\u0449\\u0435 \\u043d\\u0438\\u0433\\u0434\\u0435 \\u043d\\u0435 \\u0432\\u0438\\u0434\\u0438\\u0442 \\u043a\\u0440\\u043e\\u043c\\u0435 \\u043a\\u0430\\u043a \\u0432 \\u043f\\u0440\\u043e\\u0433\\u0440\\u0430\\u043c\\u043c\\u0430\"}{\"user\":\"DeTray\",\"time\":\"23:07\",\"text\":\"\\u0431\\u043b\\u0438\\u043d.\\u0443\\u0441\\u0442\\u0430\\u043d\\u043e\\u0432\\u0438\\u043b \\u043b\\u043e\\u043a\\u0435\\u0440 \\u0441 \\u0445\\u043e\\u043d\\u043e\\u0440\\u0430 \\u043d\\u0430 8230\"}{\"user\":\"DeTray\",\"time\":\"23:07\",\"text\":\"\\u0432 \\u043f\\u043e\\u0441\\u043b\\u0435\\u0434\\u043d\\u0435\\u043c \\u043c\\u043e\\u0434\\u0435 \\u0441\\u0442\\u043e\\u044f\\u043b\\u0430  \\u044d\\u0442\\u0430 \\u0432\\u0435\\u0440\\u0441\\u0438\\u044f\"}{\"user\":\"monksis97\",\"time\":\"23:05\",\"text\":\"Hitmanby, \\u043a\\u0430\\u043a\\u0430\\u044f \\u0432\\u0435\\u0440\\u0441\\u0438\\u044f? \\u0441\\u0442\\u0430\\u0432\\u044c 3.4.4! \\u043e\\u043d\\u0430 \\u0434\\u0430\\u0436\\u0435 \\u043d\\u0430 \\u0435\\u0436\\u0435 \\u043d\\u043e\\u0440\\u043c\\u0430\\u043b\\u044c\\u043d\\u043e \\u0440\\u0430\\u0431\\u043e\\u0442\\u0430\\u0435\\u0442)\"}{\"user\":\"chuncho180676\",\"time\":\"23:01\",\"text\":\"set dns\"}{\"user\":\"chuncho180676\",\"time\":\"23:01\",\"text\":\"\\u043a\\u0430\\u0436\\u0435\\u0442\\u0441\\u044f set dbs \\u043d\\u0430\\u0437\\u044b\\u0432\\u0430\\u0435\\u0442\\u0441\\u044f\"}]";
        Matcher matcher = messagesPattern.matcher(text);
        if (matcher.find()) {
            String message = matcher.group(3);
            Pattern pattern = Pattern.compile("\\\\u([0-9A-Fa-f]{4})");
            Matcher matcher1 = pattern.matcher(message);
            while (matcher1.find()) {
                message = matcher1.replaceFirst(String.format("%c", (char) Integer.parseInt(matcher1.group(1), 16)));
                // group(1)??????[0-9A-F]{4}?16????char??????????(replaceFirst)
                matcher1 = pattern.matcher(message);
            }
            System.err.println(message);
        }
    }
}
